﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace MEMDB
{
    public partial class DeleteTable : Form
    {
        SQLConnection m_conn;
        public DeleteTable(string TableInfo, SQLConnection conn)
        {
            InitializeComponent(); 
            m_conn = conn;

            int iIndex;
            int iTableCount;

            iIndex = TableInfo.IndexOf("数据库表个数: ");
            iTableCount = int.Parse(TableInfo.Substring(iIndex + 8, 1));
             
            iIndex = TableInfo.IndexOf("表行数");
            iIndex = TableInfo.IndexOf("|", iIndex);
            iIndex = TableInfo.IndexOf("|", iIndex+1);
            iIndex++;

            for(int i = 0; i < iTableCount; i++)
            {
                int iNewIndex = TableInfo.IndexOf("|", iIndex);
                string str = TableInfo.Substring(iIndex, iNewIndex - iIndex).Trim();
                ListViewItem item = new ListViewItem();
                item.Text = str;
                item.ImageIndex = 0;
                DBNamelistlistView1.Items.Add(item);
                iIndex = TableInfo.IndexOf("|", iNewIndex+1);
                iIndex = TableInfo.IndexOf("|", iIndex+1);
                iIndex = TableInfo.IndexOf("|", iIndex+1);
                iIndex++;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DBNamelistlistView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DBNamelistlistView1.SelectedItems.Count != 0)
                deleteButton.Enabled = true;
            else
                deleteButton.Enabled = false;
        }

        private string strMsg = "";
        public string getMsg()
        {
            return strMsg;
        }
        private void deleteButton_Click(object sender, EventArgs e)
        {
            SQLCommand cmd = new SQLCommand("", m_conn);
            cmd.m_commandString = "drop table " + DBNamelistlistView1.SelectedItems[0].Text.Trim();
            try
            {
                cmd.ExecuteNonQuery();
                strMsg = cmd.getRetMsg();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("删除表错误，原因" + ex.Message);
            }
            this.Close();
        }

        private void DeleteTable_Load(object sender, EventArgs e)
        {
            DBNamelistlistView1.View = View.LargeIcon;
        }
    }
}
